package com.neusoft.customerserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.customerserver.dto.QueryCustomer;
import com.neusoft.customerserver.dto.QueryCustomerRelationship;
import com.neusoft.customerserver.dto.RespBean;
import com.neusoft.customerserver.entity.*;
import com.neusoft.customerserver.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 客户表 前端控制器
 * </p>
 *
 * @author Victor
 * @since 2021-07-15
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CustomerContactsMapper cusConMapper;
    @Autowired
    private CustomerCustomerMapper cusCusMapper;
    @Autowired
    private ContactsMapper contactsMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private CustomerMgrRelationMapper cusMgrMapper;
    @Autowired
    private CustomerHandoverMapper cusHandMapper;

    @PostMapping("/addCustomer")
    public RespBean addCustomer(@RequestBody Customer customer){
        int num = customerMapper.insert(customer);
        if(num == 1){
            return RespBean.ok(200,"Add Success",customer);
        }else {
            return RespBean.error(500,"ACCESS DENIED",customer);
        }
    }

    @PostMapping("/updateCustomer")
    public RespBean updateCustomer(@RequestBody Customer customer){
        int num1 = customerMapper.updateById(customer);
        int num2 = 0;
        QueryWrapper<CustomerHandover> qw = Wrappers.query();
        qw.eq("customer_id",customer.getCustomerId());
        List<CustomerHandover> cusHandList = cusHandMapper.selectList(qw);
        for (CustomerHandover cusHand : cusHandList){
            cusHand.setCustomerName(customer.getCustomerName());
            num2 += cusHandMapper.updateById(cusHand);
        }
        if(num1 == 1 && num2 == cusHandList.size()){
            return RespBean.ok(200,"Update Success",customer);
        }else {
            return RespBean.error(500,"ACCESS DENIED",customer);
        }
    }

    @PostMapping("/query")
    public RespBean queryCustomer(@RequestBody QueryCustomer queryCustomer){
        QueryWrapper<Customer> qw = Wrappers.query();
        if (queryCustomer.getCustomerId() != null && !queryCustomer.getCustomerId().toString().equals("")){
            qw.like("customer_id",queryCustomer.getCustomerId());
        }
        if (queryCustomer.getCustomerName() != null && !queryCustomer.getCustomerName().equals("")){
            qw.like("customer_name",queryCustomer.getCustomerName());
        }
        if (queryCustomer.getEnterpriseProperty() != null && !queryCustomer.getEnterpriseProperty().equals("")){
            qw.eq("enterprise_property",queryCustomer.getEnterpriseProperty());
        }
        if (queryCustomer.getCustomerMgrDept() != null && !queryCustomer.getCustomerMgrDept().equals("")) {
            qw.eq("customer_mgr_dept", queryCustomer.getCustomerMgrDept());
        }
        if (queryCustomer.getCustomerMgrName() != null && !queryCustomer.getCustomerMgrName().equals("")) {
            qw.eq("customer_mgr_name", queryCustomer.getCustomerMgrName());
        }
        List<Customer> customerList = customerMapper.selectList(qw);
        if (customerList.size() < 1){
            return RespBean.error(500,"No result",null);
        }else {
            return RespBean.ok(200,"search success",customerList);
        }

    }

    @PostMapping("/queryCustomerRelationship")
    public RespBean queryCustomerRelationship(@RequestBody QueryCustomerRelationship qcr){
        QueryWrapper<CustomerCustomer> qw = Wrappers.query();
        QueryWrapper<CustomerContacts> qw2 = Wrappers.query();
        QueryWrapper<Contacts> qw3 = Wrappers.query();
        QueryWrapper<Customer> qw6 = Wrappers.query();
        if (qcr.getCustomerId() != null){
            qw.like("relative_customer_id1",qcr.getCustomerId());
            qw2.like("customer_id",qcr.getCustomerId());
            qw6.like("customer_id",qcr.getCustomerId());
            List<CustomerContacts> cusConList = cusConMapper.selectList(qw2);
            for (CustomerContacts cusCon : cusConList){
                qw3.eq("contacts_id",cusCon.getContactsId()).or();
            }
        }
        if (qcr.getCustomerName() != null){
            qw6.like("customer_name",qcr.getCustomerName());
            List<Customer> customerList = customerMapper.selectList(qw6);
            for (Customer cus : customerList){
                qw.like("relative_customer_id1",cus.getCustomerId()).or();
                qw2.like("customer_id",cus.getCustomerId()).or();
                List<CustomerContacts> cusConList = cusConMapper.selectList(qw2);
                for (CustomerContacts cusCon : cusConList){
                    qw3.eq("contacts_id",cusCon.getContactsId()).or();
                }
            }
        }
        if (qcr.getCustomerMgrDept() != null){
            qw6.eq("customer_mgr_dept",qcr.getCustomerMgrDept());
            List<Customer> customerList = customerMapper.selectList(qw6);
            for (Customer c : customerList){
                qw.like("relative_customer_id1",c.getCustomerId()).or();
                qw2.like("customer_id",c.getCustomerId()).or();
                List<CustomerContacts> cusConList = cusConMapper.selectList(qw2);
                for (CustomerContacts cusCon : cusConList){
                    qw3.eq("contacts_id",cusCon.getContactsId()).or();
                }
            }
        }
        if (qcr.getCustomerMgrName() != null) {
            qw6.eq("customer_mgr_name", qcr.getCustomerMgrName());
            List<Customer> customerList = customerMapper.selectList(qw6);
            for (Customer c : customerList) {
                qw.like("relative_customer_id1", c.getCustomerId()).or();
                qw2.like("customer_id", c.getCustomerId()).or();
                List<CustomerContacts> cusConList = cusConMapper.selectList(qw2);
                for (CustomerContacts cusCon : cusConList) {
                    qw3.eq("contacts_id", cusCon.getContactsId()).or();
                }
            }
        }

        List<CustomerCustomer> cusCusList = cusCusMapper.selectList(qw);
        List<Contacts> contactsList = contactsMapper.selectList(qw3);
        List<Customer> customerList = customerMapper.selectList(qw6);
        if (cusCusList.size() < 1 && contactsList.size() < 1 && customerList.size() < 1){
            return RespBean.error(500,"No result",null);
        }else {
            return RespBean.ok(200,"search success",cusCusList+" && "+contactsList+" && "+customerList);
        }
    }

    @PostMapping("/queryAllCustomer")
    public RespBean queryAllCustomer(){
        List<Customer> customerList = customerMapper.selectList(null);
        if (customerList.size() < 1){
            return RespBean.error(500,"No result",null);
        }else {
            return RespBean.ok(200,"Search success!",customerList);
        }

    }

    @PostMapping("/queryCustomerIdByName")
    public RespBean queryCustomerIdByName(@RequestBody Customer customer){
        QueryWrapper<Customer> qw = Wrappers.query();
        qw.eq("customer_name",customer.getCustomerName());
        Customer c = customerMapper.selectOne(qw);
        if (c == null){
            return RespBean.error(500,"No result",null);
        }else {
            return RespBean.ok(200,"Search success!",c);
        }
    }

}

